

package metadatabrowser;

import at.fhj.itm.metadata.Column;
import at.fhj.itm.metadata.Index;
import at.fhj.itm.metadata.impl.postgres.PostgresIndex;
import at.fhj.util.Pair;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 *
 * @author Seuchter
 */
public class IndexContentProvider implements IStructuredContentProvider{


    private String buildColumnList(List<? extends Column> columns)
    {
        StringBuilder sb = new StringBuilder();
        for(Column col : columns)
        {
            sb.append(col.getName()).append(" ");
        }
        
        return sb.toString();
    }

    @Override
    public Object[] getElements(Object o)
    {
        if(!(o instanceof Index))
        {
            return null;
        }
        Index ind = (Index) o;

        List<Pair<String,String>> properties = new ArrayList<Pair<String, String>>();
        properties.add(new Pair<String, String>("Name", ind.getName()));
        properties.add(new Pair<String, String>("Table", ind.getTable().getName()));
        properties.add(new Pair<String, String>("Columns",buildColumnList(ind.getColumns())));
        if(ind instanceof PostgresIndex)
        {
            PostgresIndex pgInd = (PostgresIndex) ind;
            properties.add(new Pair<String, String>("Access Method", pgInd.getAccessMethod()));

        }

        return properties.toArray();
    }

    @Override
    public void dispose()
    {

    }

    @Override
    public void inputChanged(Viewer viewer, Object o, Object o1)
    {

    }

}
